//
//  StreamEngine.m
//  viewtest
//
//  Created by xy on 10-3-17.
//  Copyright 2010 HANDPAY. All rights reserved.
//

#import "StreamEngine.h"
#import "HPClientEngine.h"
#import "Directory.h"
#import "FileUtil.h"

@implementation StreamEngine
@synthesize memoryPool;
-(id)init{
    if (self = [super init]) {
		self.memoryPool=[NSMutableDictionary dictionaryWithCapacity:10];
	}
	return self;
}


-(NSData *)getInputFromPackage:(NSString *)urlstr{
	NSString * bundlePath=[[NSBundle mainBundle] resourcePath];
	NSString * filePath=[[urlstr hasPrefix:@"res/"] ? @"" : @"res/" stringByAppendingString:urlstr];
	filePath=[bundlePath stringByAppendingPathComponent:filePath];
	
	NSData * fileData=[NSData dataWithContentsOfFile:filePath];
	return fileData;
}
-(NSData *)getInputFromPC:(NSString *)urlstr{
	NSString * path=[Directory getFilePath];
	path=[path stringByAppendingPathComponent:urlstr];
	return [NSData dataWithContentsOfFile:path];
}

-(NSData *)getInputFromCache:(NSString *)urlstr{
	NSString * path=[Directory getCachePath];
	path=[path stringByAppendingPathComponent:urlstr];
	return [NSData dataWithContentsOfFile:path];
}
-(NSData *)getInputFromTemp:(NSString *)urlstr{
	NSString * path=[Directory gettempPath];
	path=[path stringByAppendingPathComponent:urlstr];
	return [NSData dataWithContentsOfFile:path];
}

-(NSData *)getInputFromMemory:(NSString *) url{
	NSData * data=nil;
	for (NSString * key in memoryPool) {
		
		if ([key isEqualToString:url]==YES) {
			data=[memoryPool objectForKey:key];
		}
	}
	return data;
}

-(NSData *)getData:(NSString *)url withType: (URLStype)type{
	switch (type) {
		case FILE_URL:
			return [self getInputFromPC:url];
		case RES_URL:
			return [self getInputFromPackage:url];
		case MEMORY_URL:
			return [self getInputFromMemory:url];
		case CACHE_URL:
			return [self getInputFromCache:url];
		case TEMP_URL:
			return [self getInputFromTemp:url];			
		default:
			break;
	}
	return nil;
}
-(void)saveData:(NSData *)data withurl:(NSString *)url withType: (URLStype)type{
	NSString * path=nil;
	switch (type) {
		case FILE_URL:
			path=[Directory getFilePath];
			path=[path stringByAppendingPathComponent:url];
			[FileUtil create:path withData:data];
			break;
		case MEMORY_URL:
			[self.memoryPool removeAllObjects];
			[self.memoryPool setObject:data forKey:url];
			break;
		case CACHE_URL:
			path=[Directory getCachePath];
			path=[path stringByAppendingPathComponent:url];
			[FileUtil create:path withData:data];
			break;
		case TEMP_URL:
			path=[Directory gettempPath];
			path=[path stringByAppendingPathComponent:url];
			[FileUtil create:path withData:data];
			break;
		default:
			break;
	}
}
-(void)PushMemory:(NSData *)data withKey:(NSString *)key{
	[self.memoryPool setObject:data forKey:key];
}
-(void)ReleaseMemory:(NSString *)key{
	//[self.memoryPool removeAllObjects];
}
-(void)DeleteCache{
}
@end
